package com.example.lastwork;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FiveFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FiveFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static FiveFragment mf;
    private String id2;
    private View rootview;
    private String TAG = "five";
    List<Map<String,String>> data_list = new ArrayList<>();

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public FiveFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FiveFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FiveFragment newInstance(String param1, String param2) {
        FiveFragment fragment = new FiveFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public static FiveFragment getFiveFragment()
    {
        if(mf == null){
            mf = new FiveFragment();
        }
        return mf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview =  inflater.inflate(R.layout.fragment_five, container, false);
        ListView gridView = rootview.findViewById(R.id.f_list);
        TextView user = rootview.findViewById(R.id.f_u_name);
        TextView id1 = rootview.findViewById(R.id.f_u_id);
        String name =null;
        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        id2 = bundle.getString("id");
        Log.i(TAG, id2);
        new Thread(){
            @Override
            public void run() {
                try {
                    UserDao userDao = new UserDao();
                    userDao.getlove(data_list,id2);
                    String name = UserDao.getuser(id2);
                    Log.i(TAG, id2);
                    Log.i(TAG, name);
                    id1.setText(id2);
                    user.setText(name);
                    Log.i(TAG,"成功");
                    SharedPreferences sp = getActivity().getSharedPreferences("rate", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("name", name);
                    editor.apply();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "失败");
                }
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        String [] from ={"name","id"};
                        int [] to = {R.id.f_name,R.id.f_id};
                        SimpleAdapter adapter = new SimpleAdapter(getContext(), data_list, R.layout.fivelistview, from, to);
                        gridView.setAdapter(adapter);
                        Log.i(TAG, "线程运行");
                    }
                });
            }

        }.start();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 跳转到另一个界面查看商品详细信息
                String id2 = data_list.get(position).get("id");
                Intent intent = new Intent(getActivity(),
                        other_person_page.class);
                intent.putExtra("id",id2);
                Log.i(TAG, "id:" +id2);
                startActivity(intent);
            }
        });
        return rootview;
    }

    public void Onclick3(View view) throws Exception {
        switch(getId())
        {
            case R.id.f_exit:
                Intent intent = new Intent(getActivity(),
                        MainActivity.class);
                startActivity(intent);

        }
    }
}